home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 548 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.2 KB  |  91 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: jpotter@falcon.lhup.edu (John E. Potter)
  3. Newsgroups: comp.std.c++
  4. Subject: const_cast
  5. Date: 26 Feb 1996 09:33:54 PST
  6. Organization: East Stroudsburg University, Pennsylvania
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4gq4g3$k9g@jake.esu.edu>
  9. NNTP-Posting-Host: isolde.mti.sgi.com
  10. X-Original-Date: 25 Feb 1996 16:58:43 GMT
  11. X-Newsreader: TIN [version 1.2 PL2]
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBVAwUBMTHvYky4NqrwXLNJAQFd8wH9EFkN8ArDqVbKptXKZ9H1fW9jCpk6tf8R
  14.     f98ts9yDF7AXN1XYkHRnjTP5Y6fESnNKSVIeZRZJzHAbQj04ZHyC4w==
  15.     =FZ/j
  16. Originator: austern@isolde.mti.sgi.com
  17.  
  18. The following produced interesting results on several compilers.  I am not
  19. using the compilers to interpret the DWP, but am trying to use the DWP to
  20. justify the compilers and predict the future.  None of these compilers
  21. support the four new specific casts; however, I will try to explain their
  22. actions in terms of them.  The question involves the first item following
  23. return in fun.  I believe the += disambiguates to an expression statement.
  24.  
  25. struct I1 {
  26.     I1 (int v) : x(v) { }
  27.     I1& operator+= (const I1& rhs) { x += rhs.x; return *this; }
  28.     int x;
  29.     };
  30. struct I2 {
  31.     I2 (int v) : x(v) { }
  32.     I2 (const I2& src) : x(src.x) { }
  33.     I2& operator+= (const I2& rhs) { x += rhs.x; return *this; }
  34.     int x;
  35.     };
  36. int nofun (const int& s) { return (int&)(s) += 10; }
  37. int fun (const int& s) { return int(s) += 10; }
  38. I1 fun (const I1& s) { I1 t(10); return I1(s) += t; }
  39. I2 fun (const I2& s) { I2 t(10); return I2(s) += t; }
  40.  
  41.           comp1            comp2            comp3            comp4
  42. nofun     const_cast<int&> const_cast<int&> const_cast<int&> const_cast<int&>
  43.  
  44. fun(int)  const_cast<int&> static_cast<int> static_cast<int> static_cast<int>
  45.           *****            error lvalue     error lvalue     error lvalue
  46. fun(I1)   static_cast<I1>  const_cast<I1&>  static_cast<I1>  static_cast<I1>
  47.           no error         *****            no error         error lvalue
  48. fun(I2)   static_cast<I2>  static_cast<I2>  static_cast<I2>  static_cast<I2>
  49.           no error         no error         no error         no error
  50.  
  51. When I originally wrote fun, my intent was to create an unnamed temp and
  52. return a modification of it.  This requires the use of a copy constructor
  53. and I was surprised to find that I got different results when I wrote it
  54. and when I let the compiler implicitly provide it.  fun(I2) gave me what
  55. I wanted and has been permitted by many compilers.  If I read the DWP
  56. correctly, constructors return rvalues and it is now illegal.  That's
  57. fine with me.  In the future, if a compiler accepts fun(I2) it will not
  58. be certified.  Or did I miss some other interpretation which would allow
  59. fun(I2)?
  60.  
  61. Assuming that fun(I2) is not legal as a static_cast, is there any way to
  62. justify the two compilers which interpreted similar expressions as
  63. const_cast?  If so, a DWP compliant compiler would also be allowed
  64. (required?) to use const_cast in fun(I2).  I usually assume that a
  65. compiler will do what I tell it with references; whenever I could do it
  66. with pointers.  All of the compilers above worked as expected with
  67. nofun(int).  None of them would allow what the two seem to be doing.
  68. Changing to pointers, I tried *&(I1)*s and was told that I could not
  69. use & with an rvalue.  They also rejected (I1)*s.  Good.  I do not assume
  70. that a compiler will only do with references what I could do with pointers.
  71. Does the compiler have enough freedom to place its implicit dereference of
  72. the lvalue (bad words, hear the thought) into the cast to produce a valid
  73. const_cast?  I hope not.
  74.  
  75. I also tried to turn fun(I1) into the same results as fun(I2) by placing
  76. an explicit use of the copy constructor prior to the cast.  Now the
  77. compiler must implicitly generate it.  That did not change anything.
  78. Does the DWP require the compiler to "remember" that it implicitly
  79. generated something?  Does an explicitly written copy constructor with
  80. the same semantics as the implicit one have different meaning?
  81.  
  82. Thanks,
  83. John
  84. ---
  85. [ To submit articles: Try just posting with your newsreader.  If that fails,
  86.                       use mailto:std-c++@ncar.ucar.edu
  87.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  88.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  89.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  90. ]
  91.